﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace _2017_wx.Handler
{
    /// <summary>
    /// JsonHandler 的摘要说明
    /// </summary>
    public class JsonHandler : IHttpHandler
    {
        /// <summary>
        /// 取得网站根目录的物理路径
        /// </summary>
        /// <returns></returns>
        public static string GetRootPath()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            if (HttpCurrent != null)
            {
                AppPath = HttpCurrent.Server.MapPath("~");
            }
            else
            {
                AppPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                    AppPath = AppPath.Substring(0, AppPath.Length - 1);
            }
            return AppPath;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            if (context.Request.QueryString["op"] == "getAs")//当前模板所有页面
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/getAs.json");
                context.Response.Write(json);
            }
            else if (context.Request.QueryString["op"] == "mgetAs")//当前模板所有页面
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/mgetAs.json");
                context.Response.Write(json);
            }
            else if (context.Request.QueryString["op"] == "weixinShare")//
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/weixinShare.json");
                context.Response.Write(json);
            }
            else if (context.Request.QueryString["op"] == "GetStatCNum")//
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/GetStatCNum.json");
                context.Response.Write(json);
            }
            else if (context.Request.QueryString["op"] == "GetSceneCopyright")//
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/GetSceneCopyright.json");
                context.Response.Write(json);
            }
            else if (context.Request.QueryString["op"] == "GetSceneDomain")//
            {
                string basePath = GetRootPath();
                string json = File.ReadAllText(basePath + "Json/GetSceneDomain.json");
                context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}